<script setup>
import { ref } from 'vue'
import useUserInfoStore from '@/stores/userInfo';
const userInfoStore = useUserInfoStore();
const userInfo = ref({ ...userInfoStore.info })

//定义数据模型
const delData = ref({
    password: '',
    email: ''
})

//定义表单校验规则
const rules = {
    email: [
        { required: true, message: '请输入用户邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ]
}

//注销用户
import { deleteUserService } from '@/api/user';
import { ElMessage } from 'element-plus';
import { useTokenStore } from '@/stores/token';
import router from '@/router';
const tokenStore = useTokenStore()
import { ElMessageBox } from 'element-plus';
const delUser = () => {
    ElMessageBox.confirm(
        '是否确定注销账号，注销后不可恢复哦',
        '警告',
        {
            confirmButtonText: '确定注销',
            cancelButtonText: '取消注销',
            type: 'warning',
        }
    )
        .then(async () => {
            //调用接口
            let result = await deleteUserService(delData.value);
            tokenStore.removeToken()
            userInfoStore.removeInfo()
            router.push('/login')
            ElMessage({
                type: 'success',
                message: '账号已成功注销',
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消注销账号',
            })
        })
}

</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>注销账号</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="delData" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="用户名">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="delData.email" type="email"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="delData.password" type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="delUser">确定注销</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>